import { reqCartadd, reqGetgoodsinfo } from "../../http/api"
import {Toast} from "antd-mobile"

// 数据
const initState = {
    // 1.1初始化数据
    goods: {}
}


//action types
const types = {
    // 1.2 修改type
    DETAIL_CHANGE_INFO:"DETAIL_CHANGE_INFO"
}


// actions creater
export const actions = {
    // 1.4
    changeGoods:goods=>({type:types.DETAIL_CHANGE_INFO,goods}),
    // 1.6
    reqGetGOODAction:(id)=>{
        return (dispatch,getState)=>{
            reqGetgoodsinfo({id}).then(res=>{
                if(res.data.code===200){
                    // 修改banner的准备
                    dispatch(actions.changeGoods(res.data.list[0]))
                }
            })
        }
    },
    // 加入购物车
    goShop:(params)=>{
        return ()=>{
            reqCartadd(params).then(res=>{
                if(res.data.code===200){
                    Toast.success(res.data.msg,1)
                }
            })
        }
    }
}


// reducer
export const detailReducer = (state = initState, action) => {
    switch(action.type){
        // 1.3
        case types.DETAIL_CHANGE_INFO:
            return {
                ...state,
                goods:action.goods
            }
            default:
                return state;
    }
}


// 导出数据 一堆函数
// 1.5
export const getGoods=state=>state.detail.goods